<template>
  <div class="userBox">
     <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/goods' }"
        >商品管理</el-breadcrumb-item
      >
      <el-breadcrumb-item :to="{ path: '/goods' }"
        >商品列表</el-breadcrumb-item
      >
    </el-breadcrumb>
    <el-card class="box-card"> 
      <el-row type="flex">
        <!-- 搜索 -->
        <el-col :span="12">
          <div class="grid-content bg-purple title" style="text-align: left">
            <el-input
              clearable
              placeholder="请输入内容"
              class="input-with-select"   
              v-model.trim="params.query"  
              @clear="getSearchGoods"
              @keydown.native.enter="getSearchGoods"   
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getSearchGoods"
              ></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col :span="12">
          <el-row type="flex">
            <el-col :span="2"></el-col>
            <el-col :span="20">
              <el-button type="primary"
                >添加商品</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <el-table :data="tableData" border style="width: 100%  margin-top:10px">
        <el-table-column type="index" label="#" width="50" align="center">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"  align="center">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100" align="center">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100" align="center">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="180" align="center">
          <template slot-scope="scope">
             {{scope.row.add_time |formDate}}
           </template>
        </el-table-column>
        <el-table-column label="操作" align="center" >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handelEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handelDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
        
      </el-table>
    </el-card>
    <!--  分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[5, 10]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalpage"
    >
    </el-pagination>
     <!--  编辑用户弹框 -->
    <el-dialog title="编辑商品" :visible.sync="editeDialogFormVisible">
      <el-form :model="editeForm" ref="form" :rules="rules">
        <el-form-item
          label="商品名称"
          prop="goods_name"
          :label-width="formLabelWidth"
        >
          <el-input v-model="editeForm.goods_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="商品价格"
          prop="goods_price"
          :label-width="formLabelWidth"
        >
          <el-input v-model="editeForm.goods_price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="商品重量"
          prop="goods_weight"
          :label-width="formLabelWidth"
        >
          <el-input v-model="editeForm.goods_weight" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="editeDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoods">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getGoods,getDeleteGoods,getEditGoods,getQueryGoodsById} from "../../api/index";
export default {
  data(){
    return{
      tableData: [],
      totalpage: 0, //总页数
      params: {
        total: "",
        pagenum: 1,
        pagesize: 5,
      },
      //编辑商品数据定义
      editeDialogFormVisible: false,
      editeForm: {
        // id:"",
       goods_name: "",
       goods_price: "",
       goods_weight:"",
       goods_cat:"",
       goods_weight:""
      },
      //验证规则
      formLabelWidth: "200px",
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],

        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
      },
    }
  },
  methods:{
    async initGetGoods(){
      let {data:{goods,total},meta:{msg,status}} = await getGoods(this.params)
      // console.log(goods);
      this.tableData = goods;
      if (status != 200) return this.$message.error("请求失败");
      this.totalpage = total;
    },
    //分页功能
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.pagesize = val;
      this.initGetGoods();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.pagenum = val;
      this.initGetGoods();
    },
    //搜索功能
    getSearchGoods() {
      this.initGetGoods();
    },
    //删除
    async handelDelete(item){
      // console.log(item);
      let res = await this.$confirm("此操作将永久删除该商品, 是否继续?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch(() => {}); // 这里的取消必须加上一个catch 的回调函数
      if (res != "confirm") return this.$message.info("取消删除");
      let {
        meta: { status, msg },
      } = await getDeleteGoods({ id: item.goods_id});
      if (status == 200) {
        this.$message.success("删除成功");
        this.initGetGoods();
      } else {
        this.$message.error("删除失败");
      }
    },
    //编辑
    async handelEdit(item) {
      let res = await getQueryGoodsById({ id:item.goods_id} );
      // console.log(res);
      let {
        data,
        meta: { status },
      } = res;
      // console.log(data);
      // console.log(res);
      if (status != 200) return this.$message.info("查询失败了");
      this.editeForm = data;
      // console.log(data);
      this.editeDialogFormVisible = true;
    },
    async editGoods() {
      // console.log(this.editeForm);
      let {
        meta:{status, msg}
      } = await getEditGoods(this.editeForm);
      // console.log(status);
      // if (status != 201) return this.$message.info(msg);

      this.editeDialogFormVisible = false;
      this.initGetGoods();
    },
  },
  created(){
    this.initGetGoods()
  }
}
</script>

<style scoped>
  .box-card {
  width: 1200px;
}
.userBox {
  padding-left: 20px;
  margin-top: 20px;
  overflow: auto;
}
.el-pagination {
  margin: 20px auto;
  text-align: center;
}
</style>